package com.mlk.engUser.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mlk.core.exception.GlobalException;
import com.mlk.core.util.ServerResponse;
import com.mlk.engUser.mapper.EngUserBankCardMapper;
import com.mlk.engUser.po.EngUserBankCard;
import com.mlk.engUser.service.EngUserBackCardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EngUserBackCardServiceImpl implements EngUserBackCardService {

    @Resource
    private EngUserBankCardMapper engUserBankCardMapper;

    /**
     * 添加用户银行卡
     * @param card
     * @return
     */
    @Override
    public ServerResponse add(EngUserBankCard card) {
        Integer count = this.engUserBankCardMapper.insert(card);
        if(count == 0){
            throw new GlobalException("绑定银行卡失败");
        }
        return ServerResponse.ok("绑定银行卡成功");
    }

    /**
     * 查询用户所绑定的银行卡
     * @param openId
     * @return
     */
    @Override
    public ServerResponse userCards(String openId) {
        List<EngUserBankCard> cards = this.engUserBankCardMapper.selectList(new EntityWrapper<>(EngUserBankCard.builder().openId(openId).build()));
        return ServerResponse.ok(cards);
    }

    /**
     * 银行卡的修改
     * @param card
     * @return
     */
    @Override
    public ServerResponse editCard(EngUserBankCard card) {
        int  count = this.engUserBankCardMapper.updateById(card);
        if(count == 0){
            throw new GlobalException("银行卡信息修改失败");
        }

        return ServerResponse.ok("修改银行卡信息成功");
    }


    /**
     * 删除银行卡信息
     * @param id
     * @return
     */
    @Override
    public ServerResponse del(Integer id) {
        int count =  this.engUserBankCardMapper.deleteById(id);

        if(count == 0){
            throw new GlobalException("删除失败");
        }

        return ServerResponse.ok("删除成功");
    }

}
